package com.wwwarehouse.resource_center.adapter.rules;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.goods.GoodsAttributesBean;
import com.wwwarehouse.resource_center.bean.goods.GoodsSelectLabelBean;
import com.wwwarehouse.resource_center.bean.rules.TaskUnitBean;
import com.wwwarehouse.resource_center.eventbus_event.RulesEvent;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import de.greenrobot.event.EventBus;

/**
 * @author Joseph on 2017/8/25.
 *         <p/>
 *         选择属性
 */

public class GoodsSelectAttributeAdapter extends RecyclerView.Adapter<GoodsSelectAttributeAdapter.ViewHolder> {

    private Context mContext = null;
    private int mHeight = 0;
    private int mWidth = 0;
    private OnItemClickListener mListener = null;
    private List<GoodsAttributesBean.ListBean> tagList = new ArrayList<>();
    private boolean isSelected = false;
    public GoodsSelectAttributeAdapter(Context context, List<GoodsAttributesBean.ListBean> list, int height, int Width) {
        this.mContext = context;
        this.mHeight = height;
        this.mWidth = Width;
        this.tagList = list;

    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_rule_select_obj_adapter, parent, false);
        ViewHolder vh = new ViewHolder(view);
        return vh;

    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        //强制关闭复用
        holder.setIsRecyclable(false);
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.mTextView.getLayoutParams();
        params.height = (mHeight  - ConvertUtils.dip2px(mContext,126)-ConvertUtils.dip2px(mContext,50))/ 8; //RecyclerView高度减去item间距再减去距离按钮的高度
        //获取当前控件的布局对象
//        params.width = (int) ((mWidth - 4* ConvertUtils.dip2px(mContext, 10)) /3L);//设置当前控件布局的宽度和margin
        params.width =  mWidth  /3;//设置当前控件布局的宽度和margin
        holder.mTextView.setLayoutParams(params);
        holder.mTextView.setText(tagList.get(position).getAttributeName());
        holder.itemView.setTag(tagList.get(position));
         holder.mTextView.setSelected(tagList.get(position).isSelect()?true:false);
        holder.mTextView.setClickable(tagList.get(position).isFromCategory()?false:true); //来自类目的属性不可取消
        holder.mTextView.setEnabled(tagList.get(position).isFromCategory()?false:true); //来自类目的属性不可取消
        if (position==tagList.size()-1){
            EventBus.getDefault().post(new RulesEvent(1, "notify")); //通知主页面刷新
        }
        holder.mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.mTextView.setSelected(!holder.mTextView.isSelected());
                if (holder.mTextView.isSelected()){
                    tagList.get(position).setSelect(true);
                }else {
                    tagList.get(position).setSelect(false);
                }
                EventBus.getDefault().post(new RulesEvent(1,tagList.get(position),"notify"));
            }
        });

    }
    @Override
    public int getItemCount() {
        return tagList.size();
    }
    //刷新页面 ,传入已经选中的数据
    public void refreshPage(List<GoodsAttributesBean.ListBean> list) {
        for (int i1 = 0; i1 < list.size(); i1++) {
            for (int i2 = 0; i2 < tagList.size(); i2++) {
                if (list.get(i1).getAttributeUkid()==tagList.get(i2).getAttributeUkid()){
                    if (!list.get(i1).isSelect()){
                        tagList.get(i2).setSelect(false);
                        break;
                    }else if (list.get(i1).isSelect()){
                        tagList.get(i2).setSelect(true);
                        break;
                    }
                }else if (list.get(i1).getAttributeUkid()!=tagList.get(i2).getAttributeUkid()){

                }
            }
        }
        List<GoodsAttributesBean.ListBean> listBeen = removeDuplicate(tagList);
        tagList.clear();
        tagList.addAll(listBeen);
        LogUtils.showInfoLog("refreshPage");
        notifyDataSetChanged();
    }

    //去重
    private List<GoodsAttributesBean.ListBean> removeDuplicate(List<GoodsAttributesBean.ListBean> list) {
        Set<GoodsAttributesBean.ListBean> set = new HashSet<>();
        List<GoodsAttributesBean.ListBean> newList = new ArrayList<>();
        for (Iterator<GoodsAttributesBean.ListBean> iter = list.iterator(); iter.hasNext();) {
            GoodsAttributesBean.ListBean element = iter.next();
            if (set.add(element))
                newList.add(element);
        }
        return newList;
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTextView;

        public ViewHolder(View view) {
            super(view);
            mTextView = (TextView) view.findViewById(R.id.tv);
        }
    }
    public interface OnItemClickListener {
//        void ItemClickListener(List<TaskUnitBean.TaskUnitsBean> tagList);
    }

    public List<GoodsAttributesBean.ListBean> getSelectData(){
        return tagList;
    }
}
